package com.example.kai.uno.Animation_begin;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.kai.uno.Login;
import com.example.kai.uno.R;
import com.example.kai.uno.guide.guide_page1;
import com.example.kai.uno.guide.welcome_page;

public class Animation_begin extends AppCompatActivity implements View.OnTouchListener ,Runnable{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_begin);

        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        final LinearLayout tv_lin= (LinearLayout) findViewById(R.id.text_lin);//要显示的字体
        final LinearLayout tv_hide_lin= (LinearLayout) findViewById(R.id.text_hide_lin);//所谓的布
        ImageView logo= (ImageView) findViewById(R.id.image);//图片
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.splash);
        logo.startAnimation(animation);//开始执行动画
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //第一个动画执行完后执行第二个动画就是那个字体显示那部分
                animation=AnimationUtils.loadAnimation(Animation_begin.this,R.anim.text_splash_position);
                tv_lin.startAnimation(animation);
                animation=AnimationUtils.loadAnimation(Animation_begin.this,R.anim.text_canvas);
                tv_hide_lin.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        TextView tv = (TextView)findViewById(R.id.textView2);
        tv.setOnTouchListener(this);

        //启动一个延迟线程
        new  Thread(this).start();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Intent intent = new Intent(this,SecondActivity.class);
        intent.putExtra("message","欢迎来到UNO登录界面！");
        startActivity(intent);
        return true;

    }

    @Override
    public void run() {
        try{
            //延迟1秒时间
            Thread.sleep(5000);
            SharedPreferences preferences= getSharedPreferences("count", 0); // 存在则打开它，否则创建新的Preferences
            int count = preferences.getInt("count", 0); // 取出数据

            //如果用户不是第一次使用则直接调转到显示界面,否则调转到引导界面

            if (count == 0) {
                Intent intent1 = new Intent();
                intent1.setClass(Animation_begin.this, guide_page1.class);
                startActivity(intent1);
            } else {
                Intent intent2 = new Intent();
                intent2.setClass(Animation_begin.this, Login.class);
                startActivity(intent2);
            }
            finish();

            //实例化Editor对象
            SharedPreferences.Editor editor = preferences.edit();
            //存入数据
            editor.putInt("count", 1); // 存入数据
            //提交修改
            editor.commit();
        } catch (InterruptedException e) {

        }
    }
}
